package com.five.dao.impl.backstage;

import com.five.dao.backstage.TypeDao;
import com.five.entity.backstage.Type;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository
public class TypeDaoImpl implements TypeDao {


    @Override
    public int add(Type t) {
       if (t.getId()==null){ String sql="insert into t_type(name,yj,money) values(?,?,?)";
           return DBUtil.manage(sql,t.getName(),t.getYj(),t.getMoney());
       }else{
           String sql="update t_type set name=?,yj=?,money=? where id=?";
           return DBUtil.manage(sql,t.getName(),t.getYj(),t.getMoney(),t.getId());
       }
    }


    //获取数据总条数
    @Override
    public int getCount(Type type) {
        String sql="select count(*) a from t_type where 1=1 ";
        if (type.getId()!=null){
            sql+=" and id="+type.getId()+" ";
        }
        if (type.getName()!=null && type.getName()!=""){
            sql+=" and name like '%"+type.getName()+"%' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        Map map = maps.get(0);
        String s = map.get("a") + "";
        int i2 = Integer.parseInt(s);
        return i2;
    }

    @Override
    public List<Map> inqAll(Integer page, Integer limit, Type type) {
        //String sql="select * from t_type where 1=1 ";
        String sql="select tt.id id,tt.name name,tt.yj yj,tt.money money,count(tr.typeid)count,tt.status status from\n" +
                " t_type tt left join t_room tr on tt.id=tr.typeid  where 1=1 ";
        if (type.getId()!=null){
            sql+=" and tt.id="+type.getId()+" ";
        }
        if (type.getName()!=null && type.getName()!=""){
            sql+=" and tt.name like '%"+type.getName()+"%' ";
        }
        sql+=" GROUP BY tr.typeid ORDER BY tt.id  limit ?,?";
        return DBUtil.queryMap(sql,(page-1)*limit,limit);
    }

    @Override
    public int delType(Type type) {
    String sql= "delete from  t_type where id=?";
        return DBUtil.manage(sql,type.getId());
    }

    @Override
    public int bad(Integer id) {
        String sql="update t_type set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_type set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

}
